package com.xiaolei.library.Events;

import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.view.GestureDetectorCompat;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 给RecyclerView一系列对应的触摸发生的事件
 */
public abstract class RecyclerViewItemTouchListener extends RecyclerView.SimpleOnItemTouchListener implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener
{
    // 手势事件帮助类
    private GestureDetectorCompat mGestureDetector;
    private RecyclerView recyclerView;

    public RecyclerViewItemTouchListener(RecyclerView recyclerView)
    {
        this.mGestureDetector = new GestureDetectorCompat(recyclerView.getContext(), this);
        this.recyclerView = recyclerView;
    }

    @Override
    public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e)
    {
        return mGestureDetector.onTouchEvent(e);
    }

    @Override
    public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e)
    {
       
    }
    // 触摸事件

    // 抬起，手指离开触摸屏时触发(长按、滚动、滑动时，不会触发这个手势) 
    @Override
    public boolean onSingleTapUp(MotionEvent e)
    {
        View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
        if (childView != null)
        {
            int position = recyclerView.getChildLayoutPosition(childView);
            onItemClick(childView, position);
            return true;
        }
        return false;
    }


    // 长按，触摸屏按下后既不抬起也不移动，过一段时间后触发
    @Override
    public void onLongPress(MotionEvent e)
    {
        View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
        if (childView != null)
        {
            int position = recyclerView.getChildLayoutPosition(childView);
            onItemLongPress(childView, position);
        }
    }

    // 滚动，触摸屏按下后移动
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
    {
        return false;
    }

    // 滑动，触摸屏按下后快速移动并抬起，会先触发滚动手势，跟着触发一个滑动手势
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        return false;
    }

    // 短按，触摸屏按下后片刻后抬起，会触发这个手势，如果迅速抬起则不会
    @Override
    public void onShowPress(MotionEvent e)
    {
    }

    // 单击，触摸屏按下时立刻触发
    @Override
    public boolean onDown(MotionEvent e)
    {
        return false;
    }

    // 双击，手指在触摸屏上迅速点击第二下时触发
    @Override
    public boolean onDoubleTap(MotionEvent e)
    {
        View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
        if (childView != null)
        {
            int position = recyclerView.getChildLayoutPosition(childView);
            onItemDoubleClick(childView, position);
            return true;
        }
        return false;
    }

    // 双击的按下跟抬起各触发一次
    @Override
    public boolean onDoubleTapEvent(MotionEvent e)
    {
        return false;
    }

    // 单击确认，即很快的按下并抬起，但并不连续点击第二下
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e)
    {
        return false;
    }


    /**
     * 子项的双击事件
     *
     * @param itemView 长按的子控件
     * @param position 长按的下标
     */
    public abstract void onItemDoubleClick(@NonNull View itemView, int position);

    /**
     * 子项的长按事件
     *
     * @param itemView 长按的子控件
     * @param position 长按的下标
     */
    public abstract void onItemLongPress(@NonNull View itemView, int position);

    /**
     * 点击事件
     *
     * @param itemView 点击的子控件
     * @param position 点击的下标
     */
    public abstract void onItemClick(@NonNull View itemView, int position);

}
